package rexsee.ebook;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;

public class TextLine implements LineInterface {
	String indexTitle = null;
	private final String mText;
	private final int mLineHeight;
	private final Paint mPaint;
	public TextLine(String text, int lineHeight, Paint paint) {
		mText = text;
		mLineHeight = lineHeight;
		mPaint = paint;
	}
	@Override
	public boolean isNull() {
		return mText == null;
	}
	@Override
	public boolean isPageBreak() {
		return false;
	}
	@Override
	public int getHeight() {
		return mLineHeight;
	}
	@Override
	public void draw(Canvas canvas, Rect rect) {
		if (isNull()) return;
		canvas.drawText(mText, 0, mText.length(), getOriginX(rect, mPaint), getOriginY(rect, mPaint), mPaint);
	}
	public static int getOriginX(Rect rect, Paint paint) {
		Align align = paint.getTextAlign();
		if (align.equals(Align.CENTER)) {
			return rect.left + rect.width() / 2;
		} else if (align.equals(Align.RIGHT)) {
			return rect.right;
		} else {
			return rect.left;
		}
	}
	public static int getOriginY(Rect rect, Paint paint) {
		return rect.bottom - Math.round((rect.height() - paint.getTextSize()) / 2);
	}
	@Override
	public String getIndexTitle() {
		return indexTitle;
	}
	@Override
	public String getText() {
		return mText;
	}
}
